﻿var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
/*------------1.11-----------------*/
var multer = require('multer');
var mongoose = require('mongoose');
var session = require('express-session');
/*-----------------------------*/


var routes = require('./routes/index');
var users = require('./routes/users');
var upload = require('./routes/upload');

/*--------------1.11--------------------*/
global.user = require('./app/models/user');
global.db = mongoose.connect("mongodb://localhost:27017/nodedb");
global.save = require('./database/save');
/*----------------------------------*/
var app = express();
/*--------------1.11--------------------*/
//初始化session
app.use(session({
  secret: 'secret',
  cookie:{
    maxAge: 1000*60*30  //设置session的过期时间，如果session存储在mongodb中的话，任何低于60s(60000ms)的设置是没有用的
}
}));

app.use(function(req,res,next){
  res.locals.user = req.session.user;   // 从session 获取 user对象
  var err = req.session.error;   //获取错误信息
  delete req.session.error;
  res.locals.message = "";   // 展示的信息 message
  if(err){
    res.locals.message = '<div class="alert alert-danger" role="alert" style="margin-bottom:20px;color:red;">'+err+'</div>'
  }
  next();  //中间件传递
});
/*----------------------------------*/

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
app.engine('.html',require('ejs').__express);//两个下划线
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
/*---------------1.11------------------*/
app.use(multer({dest: "./public/avatar"}));
/*-------------------------------------*/
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'bower_components')));

app.use('/', routes);

app.use('/login', routes);
//
////filter
//app.use('condition', function(req, res, next){
//  if (session){
//    next();
//  }else{
//    res.redirect('404');
//  }
//});

app.use('/users', users);

app.use('/logout', routes);
app.use('/home', routes);
app.use('/register', routes);
app.use('/upload', upload);


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});


module.exports = app;
